﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;
using System.Text;

public partial class Pages_close_wip_entry : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ButtonSaveWIP_Click(object sender, EventArgs e)
    {
        
        // first check for input validation
        if (!(Common.IsRightDecimalEntry(TextBox2.Text.ToString())))
        {
            Label1.Text = "WIP value is not in correct format";
            return;
        }

        // if a row in manufact of this month is available and
        // if the OK field is not true then only save can be performed

        Label1.Text = "";

        // if the balance sheet is saved for this month, no entry is possible

        DataSet ds = new DataSet();
        string query = "select OK from MANUFACT where year_code=" + Convert.ToDateTime(TextBoxDate.Text.ToString()).Year.ToString() + " and month_type=" + Convert.ToDateTime(TextBoxDate.Text.ToString()).Month.ToString();
        Common.Getdata(ds, query, "okvalue");

        if (ds.Tables["okvalue"].Rows.Count < 1)
            Label1.Text = "Data cannot be saved in this future time";
        else if ( ds.Tables["okvalue"].Rows[0][0] == DBNull.Value)
            Label1.Text = "OK";
        else if    (Convert.ToInt32(ds.Tables["okvalue"].Rows[0][0]) == 1)
        {
            Label1.Text = "Data cannot be saved in this month,year of saved balanced sheet";
            return;
        }

        string conString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
        SqlCommand myCommInsert = new SqlCommand();
        myCommInsert.Connection = new SqlConnection(conString);
        myCommInsert.Connection.Open();

        string strQuery = "UPDATE MANUFACT SET [CLOSE_WIP] = " + TextBox2.Text.ToString() + " WHERE [MONTH_TYPE] = " + Convert.ToDateTime(TextBoxDate.Text.ToString()).Month.ToString() + " and [YEAR_CODE] = " + Convert.ToDateTime(TextBoxDate.Text.ToString()).Year.ToString();
        myCommInsert.CommandText = strQuery;
        myCommInsert.ExecuteNonQuery();

    }
}
